package com.example.universitylibraryback.pipeline.filter;

import com.example.universitylibraryback.pipeline.BorrowContext;
import com.example.universitylibraryback.pipeline.Filter;
import java.util.concurrent.BlockingQueue;

/**
 * 图书状态更新过滤器
 * 负责更新图书的借阅状态
 */
public class BookStatusUpdateFilter implements Filter<BorrowContext>, Runnable {
    private BlockingQueue<BorrowContext> inputQueue;
    private BlockingQueue<BorrowContext> outputQueue;

    @Override
    public void process(BlockingQueue<BorrowContext> inputQueue, BlockingQueue<BorrowContext> outputQueue) throws InterruptedException {
        this.inputQueue = inputQueue;
        this.outputQueue = outputQueue;
        new Thread(this).start();
    }

    @Override
    public void run() {
        try {
            while (!Thread.currentThread().isInterrupted()) {
                BorrowContext context = inputQueue.take();
                if (!context.isValid()) {
                    outputQueue.put(context);
                    continue;
                }
                System.out.printf("DEBUG：图书[%s]状态更新为'在借'%n", context.getBookId());
                context.setErrorMsg(context.getErrorMsg() + "，图书状态已更新");
                outputQueue.put(context);
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            System.err.println("BookStatusUpdateFilter中断");
        }
    }
}